package com.general.template.controller;


import com.general.template.enums.ResultEnum;
import com.general.template.service.PowerAdminService;
import com.general.template.utils.Result;
import com.general.template.vo.AdminVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 杰哥
 * @Description:
 **/
@RestController
@RequestMapping("admin")
@Api(value="管理员账号管理",description = "对管理员账号信息的管理")
public class AdminController {

    @Autowired
    private PowerAdminService powerAdminService;

//    @Autowired
//    private RedisTemplate redisTemplate;
//
//    /**
//     * 需要修改,自行创建类
//     * 登陆校验
//     * @param user
//     * @return
//     */
//    @RequestMapping(value = "/login",method = RequestMethod.POST)
//    @ResponseBody
//    public Result<Object> login(@RequestBody UserLoginVo user){
//        if (user == null || org.apache.commons.lang3.StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
//            return new Result<>(ResultEnum.PARAMS_NULL.getCode(), "用户名或密码错误！");
//        }
//        Subject subject = SecurityUtils.getSubject();
//        AuthenticationToken authenticationToken = new UsernamePasswordToken(user.getUsername(),user.getPassword(), StateEnum.ADMIN.getCode());
//        try{
//            subject.login(authenticationToken);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result<>(ResultEnum.PARAMS_NULL.getCode(),"用户名或密码错误!");
//        }
//        //登陆成功
//        Serializable id = subject.getSession().getId();
//        PartyAdmin principal = (PartyAdmin) subject.getPrincipal();
//        Map<String,Object> map = new HashMap<>(2);
//        principal.setPassword("");
//        redisTemplate.boundValueOps(RedisKeyConstant.TOKEN_KEY+":"+id).set(principal,60*60*3, TimeUnit.SECONDS);
//        map.put("user",principal);
//        map.put("token",id);
//        Result<Object> result = new Result<>(map);
//        result.setCode(ResultEnum.SUCCESS.getCode());
//        result.setMsg(ResultEnum.SUCCESS.getMsg());
//        return result;
//    }

    @RequestMapping(value = "updateInfo",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改管理员信息",notes = "修改管理员信息")
    public Result<Object> updateInfo(@RequestBody AdminVo admin){
        int val = powerAdminService.updateAdmin(admin);
        if (val==0){
            return new Result<>(ResultEnum.SUCCESS,"修改失败!");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"修改成功!");
        }
    }

    @RequestMapping(value = "updatePwd",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改管理员密码",notes = "修改管理员密码")
    public Result<Object> updatePwd(@RequestBody AdminVo admin){
        int val = powerAdminService.updatePwd(admin);
        if (val==0){
            return new Result<>(ResultEnum.SUCCESS,"密码修改失败!");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"密码修改成功!");
        }
    }

}
